using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Open.Data.Persistence;


namespace Open.Data.Persistence.Testing
{
    [TestClass]
    public class ViewTests
    {
        //Initialise test class
        [ClassInitialize()]
        public static void ClassInitialize(TestContext testContext)
        {
            Command command = Singleton.Instance.GetContext().CreateCommand();

            Person person = new Person();
            person.Id = 50;
            person.FirstNames = "James";
            person.LastNames = "Westgate";
            person.Insert(command);

            Customer customer = new Customer();
            customer.Id = 51;
            customer.FirstNames = "John";
            customer.LastNames = "Doe";
            customer.CreditLimit = 700;
            customer.Insert(command);

            Order order = new Order();
            order.Id = 7;
            order.Insert(command);

            Product product = new Product();
            product.Id = 701;
            product.Description = "Cucumbers";
            product.Insert(command);

            OrderItem oi = new OrderItem();
            oi.OrderId = order.Id;
            oi.ProductId = product.Id;
            oi.Quantity = 2;
            oi.Insert(command);

            CustomerOrder co = new CustomerOrder();
            co.CustomerId = customer.Id;
            co.OrderId = order.Id;
            co.Insert(command);
        }

        //Simple entity tests
        [TestMethod]
        public void ViewLoadView()
        {
            Command command = Singleton.Instance.GetContext().CreateCommand();
            PersonView view = new PersonView();
            view.Id = 50;
            view.Select(command);

            Assert.IsTrue(view.FirstNames == "James", "View not loaded correctly.");
        }

        [TestMethod, ExpectedException(typeof(Open.Data.Persistence.RecordNotFoundException))]
        public void ViewRecordNotFound()
        {
            Command command = Singleton.Instance.GetContext().CreateCommand();
            PersonView view = new PersonView();
            view.Id = 9999;
            view.Select(command);
        }

        //Inherited view tests eg CustomerView
        [TestMethod]
        public void ViewLoadInheritedView()
        {
            Command command = Singleton.Instance.GetContext().CreateCommand();
            CustomerView view = new CustomerView();
            view.Id = 51;
            view.Select(command);

            Assert.IsTrue(view.FirstNames == "John", "View not loaded correctly.");
            Assert.IsTrue(view.CreditLimit == 700, "View not loaded correctly.");
        }

        //Relationship view tests
        [TestMethod]
        public void ViewLoadRelationshipView()
        {
            Command command = Singleton.Instance.GetContext().CreateCommand();
            OrderItemView view = new OrderItemView();

            view.OrderId = 7;
            view.ProductId = 701;

            view.Select(command);
            Assert.IsTrue(view.Quantity == 2, "Relationship View not loaded correctly");
        }
    }
}
